// pages/loginDetails/loginDetails.js
const app = getApp();
const db = wx.cloud.database();
const logins_col = db.collection('logins');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isDisabled: true,
        isShowBtn: "hidden",
        imgSrc: "",
        userName: "",
        phone: "",
        addressWord: "",
        isCheckone: false,
        isChecktwo: false,
        newLoginId: "",
        isShowImgone: true,
        isShowImgtwo: false,
        errorWord: "", //手机号码校验文字
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // 状态栏样式
        wx.setNavigationBarColor({
            frontColor: '#000000',
            backgroundColor: '#000000',
        });
        if(app.globalData.userInfo.avatarUrl !== ""){
            this.setData({
                imgSrc: app.globalData.userInfo.avatarUrl,
                userName: app.globalData.userInfo.nickName,
                newLoginId: options.id,
            });
        }else{
            this.setData({
                imgSrc: app.globalData.userInfo.avatarUrl,
                userName: app.globalData.userInfo.nickName,
                newLoginId: options.id,
                isShowImgone: false,
                isShowImgtwo: true
            });
        }
        this.getloginOne();
        
    },

    // 请求登录信息
    async getloginOne() {
        var oid = this.data.newLoginId;
        let res = await logins_col.doc(oid).get();
        var sexOne = res.data.sex;
        if(sexOne==="1"){
            this.setData({
                isCheckone: true,
                phone: res.data.telephone,
                addressWord: res.data.address,
                area: res.data.area,
            })
        }else{
            this.setData({
                isChecktwo: true,
                phone: res.data.telephone,
                addressWord: res.data.address,
                area: res.data.area,
            })
        };
    },

    // 修改按钮事件
    edixBtn: function() {
        if(this.data.isDisabled == false){
            this.setData({
                isDisabled: true,
                isShowBtn: "hidden"
            })
        }else{
            this.setData({
                isDisabled: false,
                isShowBtn: "visible"
            })
        }
    },

    // 退出登录
    loginOut: function() {
        wx.showModal({
            title: '提示',
            content: '您确定要退出登录吗？',
            success (res) {
              if (res.confirm) {
                app.globalData.userInfo = {};
                app.globalData.code = "";
                app.globalData.userId = "";
                wx.reLaunch({
                    url: '../../pages/my/my'
                });
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
          
          
        
    },

    // 保存信息
    submit: function(e) {
        var oid = this.data.newLoginId;
        var phoneReg = /^1[0-9]\d{9}$/;
        var newPhone = e.detail.value.phone;
        if(newPhone.length < 11 && newPhone > 0){
            this.setData({
              errorWord: "手机号码要有11位！",
            });
          }else if(newPhone.length == 0){
            this.setData({
              errorWord: "手机号码不能为空！",
            });
          }else if(!(phoneReg.test(newPhone))){
            this.setData({
              errorWord: "手机号码格式为数字！",
            });
            return false
          }else {
            this.setData({
                errorWord: ""
            });
            logins_col.doc(oid).update({
                data:{
                    telephone: e.detail.value.phone,
                    sex: e.detail.value.sex,
                    address: e.detail.value.address,
                    area: e.detail.value.area,
                },
                success: function() {
                    wx.showToast({
                        title: '保存成功',
                        icon: "success",
                        duration: 2000
                      })  
                  }
            })
        }
    }
   
})